package com.phonebrand.dao;


import java.util.List;

import android.util.Log;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Constraint;
import com.db4o.query.Query;
import com.phonebrand.model.FlagObjM;
import com.phonebrand.model.PhoneM;

public class DB4CommandDao {
	private static final String LOG_TAG = "DB4CommandDao";
	
	public static String DB_PHONE = "PHONEDB.txt";
	
	private static DB4CommandDao mInstance= null; 	
	public static synchronized DB4CommandDao getInstance(){         
	
		if(null == mInstance){                 
			mInstance = new DB4CommandDao();         
		}         
		return mInstance;     
	}

	
	
	
	public ObjectContainer openDB(String dbFile) {
		try {									
			if (dbFile.indexOf(DB_PHONE) != -1) {
				Log.i(LOG_TAG, "=======DB_WORKLIST=======");				
				EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();
				conf.common().objectClass(PhoneM.class).objectField("phoneNum").indexed(true);
				conf.common().objectClass(PhoneM.class).objectField("name").indexed(true);
				ObjectContainer db = Db4oEmbedded.openFile(conf,dbFile);
				return db;	
			} else {
				ObjectContainer db = Db4oEmbedded.openFile(dbFile);
				return db;
			}
						
		} catch (Exception e) { 
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : openDB() "+e.toString());
		}
		return null;
	}
	

	public void storePhone(PhoneM vo ,ObjectContainer db) {						
		try {						
			db.store(vo);			
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : storePhone() "+e.toString());
		}	
	}

	
	

	public void storeFlag(FlagObjM vo ,ObjectContainer db) {						
		try {						
			db.store(vo);			
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : storeFlag() "+e.toString());
		}	
	}

	
	
	
	
	public void deletePhone(ObjectContainer db) {						
		try {	
			List<PhoneM>  phoneMs = loadAllPhoneM(db);
			if ( phoneMs != null) {
				for(PhoneM vo :  phoneMs) {
					db.delete(vo);
				}
			}							
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : deletePhone() "+e.toString());
		}	
	}
	

	
	public void deletePhone(PhoneM vo ,ObjectContainer db) {						
		try {				
			db.delete(vo);									
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : deletePhone() "+e.toString());
		}	
	}
	
	
	
	public List<PhoneM> loadAllPhoneM(ObjectContainer db) {									
		try {						
			Query query = db.query();
			query.constrain(PhoneM.class);			  
			query.descend("name").orderAscending();
			 
			 return query.execute(); 			 		
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : loadAllPhoneM() "+e.toString());	
		}
		return null;
	}
	
	
	public List<PhoneM> loadAllPhoneM(String condition,ObjectContainer db) {									
		try {						
			Query query = db.query();
			query.constrain(PhoneM.class);			  
			Log.e(LOG_TAG, "condition "+condition);
			Constraint constr = query.descend("name").constrain(condition.toLowerCase()).like();
			constr = constr.or(query.descend("name").constrain(condition.toUpperCase()).like());
			query.descend("name").orderAscending();			 
			 return query.execute(); 			 		
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : loadAllPhoneM() "+e.toString());	
		}
		return null;
	}
	
	
	
	
	
	public List<FlagObjM> loadFlagObjM(ObjectContainer db) {									
		try {						
			Query query = db.query();
			query.constrain(FlagObjM.class);			  					 
			 return query.execute(); 			 		
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "DB4CommandDao : loadFlagObjM() "+e.toString());	
		}
		return null;
	}	
	
	
	
	public PhoneM loadPhoneMByNum(String number,ObjectContainer db) {									
		try {						
			Query query = db.query();
			query.constrain(PhoneM.class);			  
			//Constraint constr = query.descend("phoneNum").constrain(number).equal();			
			query.descend("phoneNum").constrain(number).equal();
			return (PhoneM)query.execute().get(0);		 			 		
		} catch (Exception e) {
		//	e.printStackTrace();
		//	Log.e("PhoneBrand", "DB4CommandDao : loadPhoneMByNum() "+e.toString());	
		}
		return null;
	}

	

	
	
}
